<template>
  <div style="width: 100%">
    <div style="padding-left: 40px;margin-top: 20px">
      <el-form :inline="true" size="mini">
        <el-form-item v-for="(item, index) in colorExplainArray" :label="item.name">
          <span class="colorBlock" :style="{'backgroundColor': item.color}"></span>
        </el-form-item>
        <el-form-item>
          <el-button style="margin-left: 20px" type="primary" @click="startTidy">开始整库</el-button>
        </el-form-item>
      </el-form>

    </div>
    <el-form :inline="true" size="mini">
      <div style="float: left">
        <div>
          <el-form-item label="一层">
            <el-checkbox v-model="isFirstFloor"></el-checkbox>
          </el-form-item>
        </div>
        <div>
          <el-form-item label="二层">
            <el-checkbox v-model="isSecFloor"></el-checkbox>
          </el-form-item>
        </div>
      </div>
      <div class="storage-block" ref="storageBlock" :style="{'height': storageHeight}">
        <div v-for="(item, index) in listCopy"
             v-if="item.layer == 1 && isFirstFloor"
             :title="`合同号 : ` + item.date + `\n` + `材料号 : ` + item.name + `\n` + `状态 : ` + item.status
              + `\n` + `所在行 : ` + item.row + `\n` + `所在列 : ` + item.col"
             :key="index"
             class="item-block"
             :style="{'width': blockWidth + `%`,
             'height':blockHeight  + `%`,
             'top':blockHeight * (parseInt(item.row) - 1) + blockTop  * (parseInt(item.row)) + `%`,
             'left':blockWidth * (parseInt(item.col) - 1) + blockLeft  * (parseInt(item.col)) + `%`,
             'background': materialArray.indexOf(item.name) > -1 ? '#74ea53': '',
             }"
             :class="[{'block-lock': (item.status == `锁定`),'block-untidy': (item.status == `未整理`),
             'block-tidy': (item.status == `已整理`)}]">

        </div>
        <div v-for="(item, index) in listCopy"
             v-if="item.layer == 2 && isSecFloor"
             :title="`合同号 : ` + item.date + `\n` + `材料号 : ` + item.name + `\n` + `状态 : ` + item.status
              + `\n` + `所在行 : ` + item.row + `\n` + `所在列 : ` + item.col"
             :key="index"
             class="item-block"
             :style="{'width': blockWidth + `%`,
             'height':blockHeight  + `%`,
             'top':blockHeight * (parseInt(item.row) - 1) + (2 / 3) * blockHeight + blockTop  * (parseInt(item.row)) + `%`,
             'left': blockWidth * (parseInt(item.col) - 1) + blockLeft  * (parseInt(item.col)) + 1 + `%`,
             'background': materialArray.indexOf(item.name) > -1 ? '#74ea53': '',
             'z-index': item.layer}"
             :class="[{'block-lock': (item.status == `锁定`),'block-untidy': (item.status == `未整理`),
             'block-tidy': (item.status == `已整理`)}]">

        </div>
        <div class="target-area"
             :style="{'width': (blockWidth + blockWidth / 3) * (targetAreaWidth + 1) + `%`,
                      'left': blockWidth * (Math.min(parseInt(targetAreaStartCol),parseInt(targetAreaEndCol)) - 1) + blockLeft  * (Math.min(parseInt(targetAreaStartCol),parseInt(targetAreaEndCol))) - blockLeft / 2+ `%`}"></div>
      </div>
      <div style="clear: both"></div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "storageInfo",
  props: {
    list: {
      type: Array,
      default: [],
    },
    materialNum: {
      type: String,
      default: ""
    },
    startCol: {
      type: String,
      default: ""
    },
    endCol: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      listCol: 4,
      listRow: 4,
      storageHeight: 0,
      listCopy: [],
      listPropertyArray: [],
      colorExplainArray: [
        {
          name: `已选中`, color: '#74ea53'
        },
        {
          name: `锁定`, color: '#909090'
        },
        {
          name: `空闲`, color: '#ffffff'
        },
        {
          name: `未整理`, color: '#22a7ff'
        },
        {
          name: `已整理`, color: '#ff3333'
        },
      ],
      isFirstFloor: true,
      isSecFloor: true,
      materialArray: [],
      targetAreaStartCol: 1,
      targetAreaEndCol: 0,
    }
  },
  watch: {
    list(val) {
      this.listCopy = val.map(item => {
        return item
      })
      this.listCopy.map((item, index) => {
        let col = parseInt(item.name.slice(5, 7))
        let row = parseInt(item.name.slice(3, 5))
        let layer = parseInt(item.name.slice(7))
        this.listCopy[index].col = col
        this.listCopy[index].row = row
        this.listCopy[index].layer = layer
      })
    },
    materialNum(val) {
      this.materialArray = []
      let array = val.split(' ')
      array.map(item => {
        if (item) {
          this.materialArray.push(item)
        }
      })
    },
    startCol(val) {
      let col = parseInt(val)
      if (col) {
        if (col < 1) {
          this.targetAreaStartCol = 1
        } else if (col > this.listCol) {
          this.targetAreaStartCol  = this.listCol
        } else {
          this.targetAreaStartCol = col
        }
      } else {
        this.targetAreaStartCol = 1
      }


    },
    endCol(val) {
      let col = parseInt(val)
      if (col) {
        if (col <= 0) {
          this.targetAreaEndCol = 0
        } else if (col > this.listCol) {
          this.targetAreaEndCol  = this.listCol
        } else {
          this.targetAreaEndCol = col
        }
      }
      else {
        this.targetAreaEndCol = this.listCol
      }

    },
  },
  computed: {
    targetAreaWidth() {
      return Math.abs(this.targetAreaEndCol - this.targetAreaStartCol)
    },
    blockWidth() {
      let width = parseInt(`100`) / parseInt(this.listCol) / 3 * 2
      return width
    },
    blockHeight() {
      let height = parseInt(`100`) / parseInt(this.listRow) / 3 * 2
      return height
    },
    blockTop() {
      let top = parseInt(`100`) / parseInt(this.listRow) / 3 * 2 / 3 * 1
      return top
    },
    blockLeft() {
      let left = parseInt(`100`) / parseInt(this.listCol) / 3 * 2 / 3 * 1
      return left
    }
  },
  methods: {
    startTidy() {
      this.$emit('startTidy')
    }
  },
  mounted() {
    this.storageHeight = this.$refs.storageBlock.clientWidth + `px`
    window.onresize = () => {
      this.storageHeight = this.$refs.storageBlock.clientWidth + `px`
    }
    this.targetAreaEndCol = this.listCol
  }
}
</script>

<style scoped>
.colorBlock {
  display: inline-block;
  height: 25px;
  width: 30px;
  border: 2px solid #909090;
  margin-right: 10px;

}

.storage-block {
  float: left;
  width: calc(40% - 100px);
  border: 2px solid #909090;
  /*padding: 15px;*/
  position: relative;
  background: #ededed;
}

.item-block {
  position: absolute;
  /*width: 30px;*/
  /*height: 25px;*/
  border: 2px solid #909090;
  cursor: pointer;
  background: white;
  border-radius: 2px;
  box-shadow: 6px 2px 1px #cdcdcd;
}

.block-lock {
  background: #909090;
  cursor: not-allowed;
}

.block-untidy {
  background: #22a7ff;
}

.block-tidy {
  background: #ff3333;
}
.target-area {
  position: absolute;
  height: 98%;
  top: 1%;
  border: 2px dashed #ff5e5e;
}
</style>
